{extend name="public:base"}
{block name='css'}
{/block}

{block name='body'}
<div class="view view-main white-navbar white-page white-toolbar">
    <div data-name="home" class="page">


        <div class="navbar">
            <div class="navbar-inner">
                <div class="left"><a href="{$my_prev_url|default='/'}" class="external"><i class="iconfont icon-fanhui"></i></a></div>
                <div class="center"><span>注册</span></div>
                <div class="right"></div>
            </div>
        </div>

        <div class="page-content">

            <div class="form row">
                <div class="col-80">
                    <form name="form" action="" id="form" method="post" >
                        <div class="row b">
                            <i class="iconfont icon-yonghu1"></i>
                            <input type="text" name="name" placeholder="请输入姓名" />
                        </div>
                        <div class="row b">
                            <i class="iconfont icon-shouji"></i>
                            <input type="number" name="tel" placeholder="请输入您的手机号" />
                        </div>
                        <div class="row b">
                            <i class="iconfont icon-yanzhengma"></i>
                            <div class="input">
                                <input type="number" name="code" placeholder="请输入验证码" />
                            </div>
                            <span class="getCode" onclick="getCode();">获取验证码</span>
                        </div>
                        <div class="row b">
                            <i class="iconfont icon-mima"></i>
                            <input type="password" name="password" id="password" placeholder="请输入密码" />
                        </div>
                        <div class="row b">
                            <i class="iconfont icon-mima"></i>
                            <input type="password" name="confirm_password" placeholder="请再次输入密码" />
                        </div>
                        <div class="row b">
                            <i class="iconfont icon-yonghu"></i>
                            <input type="number" name="invite_code" value="{:session('user.code')}" placeholder="请输入推荐人编号" />
                        </div>
                        <input type="hidden" name="is_add" value="1" />
                        <p class="row segmented-round">
                            <button onclick="" class="col button button-big button-fill " style="background:#66ba61;">提交注册</button>
                        </p>
                    </form>
                </div>
            </div>

        </div>


    </div>
</div>
{/block}

{block name="js"}
<script type="text/javascript">
    var counttime = 60;

    //获取验证码
    function getCode(){
        if ( counttime != 60 ) {
            return false ;
        }

        var tel = $("input[name='tel']").val() ;

        if ( ! isTel( tel ) ) {
            messageToast("请输入正确的手机号");
            return false ;
        }

        $.post( "{:url('index/sendCode')}" , {tel:tel,state:1}, function ( data ) {
            if ( data.status == 'y' ) {
                //ajax后才执行
                settime();
            }
            messageToast( data.info ) ;
        }, "json");

    }

    //获取验证码倒计时
    function settime() {
        if ( counttime == 0 ) {
            $('.getCode').text("获取验证码");
            counttime = 60;
            return false;
        } else {
            $('.getCode').text('重新获取('+counttime+'s)');
            counttime--;
        }
        setTimeout(function () {
            settime();
        },1000);
    }

    var ajaxOn = false ;
    $().ready(function () {
        $("#form").validate({
            rules: {
                name: "required",
                tel: "required" ,
                code: "required" ,
                password: {
                    required: true,
                    minlength: 6
                },
                confirm_password:  {
                    required: true,
                    equalTo: '#password'
                }
                // pid : "required"
            },
            messages: {
                name: '请输入昵称',
                tel: '请输入手机号',
                code: '请输入验证码',
                password: {
                    required: "请输入新密码",
                    minlength: "密码长度不能少于6位"
                },
                confirm_password: {
                    required: "请再次输入新密码",
                    equalTo: "两次密码输入不一致"
                }
                // pid : "请输入推荐人编号"
            },
            showErrors: function (errorMap, errorList) {
                var msg = "";
                $.each(errorList, function (i, v) {
                    messageToast( v.message );
                    return false;
                });
            },
            onfocusout: false,
            onsubmit : true ,
            onkeyup : false ,
            onclick : false ,
            focusInvalid:false,
            submitHandler:function(form){
                if (ajaxOn) {
                    return false ;
                }
                ajaxOn = true ;
                $.post('{:url("login/register", ["handel"=>1])}', $("#form").serialize(), function(data){
                    messageToast( data.info );
                    if (data.status == 'y') {
                        setTimeout(function(){
                            self.location = "{:url('manage')}";
                        },1000);
                    }
                    ajaxOn = false ;
                }, 'json');

                return false ;

            }
        });
    });
</script>
{/block}